//
//  WLUploadTaskCell.m
//  WanLian
//
//  Created by 123456 on 2018/12/8.
//  Copyright © 2018 wanlian. All rights reserved.
//

#import "WLUpDownloadTaskCell.h"
#import "WLFileUpDownLoadModel.h"
#import "NSString+Ext.h"
#import "XLCircleProgress.h"

@interface WLUpDownloadTaskCell ()
@property (weak, nonatomic) IBOutlet UIView *pauseOrStartView;

@property (weak, nonatomic) IBOutlet UILabel *titleLable;

@property (weak, nonatomic) IBOutlet UILabel *taskStateLabel;
@property (weak, nonatomic) IBOutlet UILabel *hasLoadedSizeLabel;
@property (weak, nonatomic) IBOutlet UILabel *totalNeedLoadSizeLabel;
@property (weak, nonatomic) IBOutlet UILabel *currentSpeedLabel;
@property (nonatomic, weak) XLCircleProgress *progressView;

@end
@implementation WLUpDownloadTaskCell

- (void)awakeFromNib {
    [super awakeFromNib];
    if (_progressView == nil) {
        XLCircleProgress *progressView = [[XLCircleProgress alloc] initWithFrame:self.pauseOrStartView.bounds];
        [self.pauseOrStartView addSubview:progressView];
        WLWkSelf
        progressView.clickActionBlock = ^(BOOL isStart) {
            [weakSelf clickAction:isStart];
        };
        _progressView = progressView;

    }
}
- (void)clickAction:(BOOL)isStart {
    if (isStart == YES) {//要开始下载
        BOOL canBeStart = NO;
        if (self.canbeStartBlock) {
            canBeStart = self.canbeStartBlock();
        }
        if (canBeStart == YES) {
            [self configStartState];
            if (self.actionBlock) {
                self.actionBlock(isStart);
            }
        } else {
            [SVProgressHUD showAndAutoCloseWithStatus:@"任务正在进行中，请先暂停其他任务" delayTime:2.f];
        }
    } else {
        self.model.loadState = FileLoadingStatePause;
        self.taskStateLabel.text = @"暂停中";
        [self configStopState];
        if (self.actionBlock) {
            self.actionBlock(isStart);
        }
    }
    
}
- (void)setModel:(WLFileUpDownLoadModel *)model {
    _model = model;
    self.titleLable.text = model.fileName;
    [self.progressView setProgress:model.process];
    switch (model.loadState) {
        case FileLoadingStateRunning:{
            
            self.taskStateLabel.text = model.isUpLoad ? @"上传中" : @"下载中";
            [self.progressView setLoadingState];
        }
            break;
        case FileLoadingStatePause:{
            self.taskStateLabel.text = @"暂停中";
            [self configStopState];
        }
            break;
        case FileLoadingStateFail:{
            
            self.taskStateLabel.text = @"失败";
            [self configStopState];
        }
            break;
        
        case FileLoadingStateWaiting:{
            self.taskStateLabel.text = @"等待中";
            [self configStopState];
        }
            break;
        default:
            break;
    }
    self.totalNeedLoadSizeLabel.text = model.fileTotalSize;
    
    self.hasLoadedSizeLabel.text = [NSString formateSizeWithBytes:model.fileTotalBytes * model.process];
    if (model.loadState != FileLoadingStateRunning) {
        self.currentSpeedLabel.text = @"";
    }
    
}

- (void)updateRealTimeSpeed:(NSString *)netSpeed {
    if (self.model.loadState == FileLoadingStateRunning) {
        self.currentSpeedLabel.text = netSpeed;
    }else {
        self.currentSpeedLabel.text = @"";
    }
}
- (void)updateProgressWithProgress:(float)progress {
    if (self.model.loadState != FileLoadingStateRunning) {
        return;
    }
    self.hasLoadedSizeLabel.text = [NSString formateSizeWithBytes:self.model.fileTotalBytes * progress];
    [self.progressView setProgress:progress];
}
- (void)updateProgressWithProgress:(float)progress receivedSize:(NSInteger)receivedSize expectedSize:(NSInteger)expectedSize {
    
}
- (void)configStartState {
    [self.progressView setLoadingState];
    if (self.model.isUpLoad) {
        self.taskStateLabel.text = @"上传中";
    } else {
        
        self.taskStateLabel.text = @"下载中";
    }
}
- (void)configStopState {
    [self.progressView setPauseState];
    self.currentSpeedLabel.text = @"";
}
@end
